package com.dodol.builder;

import com.dodol.common.BaseTestCase;
import com.dodol.common.TestStep;
import com.dodol.util.ExcelUtil;
import jxl.read.biff.BiffException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * @Auther: 80736
 * @Date: 2018/9/1 17:36
 * @Description: 测试用例生成器
 * 从excel表格内抽取数据，根据关键字解析，组成一条条测试用例
 */
public class TestCaseBuilder {

    private static ArrayList<BaseTestCase> testCases;


    public static void main(String[] args) throws IOException {
        try {
            WebDriver driver = new ChromeDriver();
            ExcelUtil.printContext("testcase.xls");
            List<String[]> data = ExcelUtil.generateTestData("testcase.xls");
            ArrayList<BaseTestCase> testcases = generateTestCases(driver, data);
        } catch (BiffException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据测试数据生成测试用例
     *
     * @param driver
     * @param data
     */
    public static ArrayList<BaseTestCase> generateTestCases(WebDriver driver, List<String[]> data) {
        testCases = new ArrayList<BaseTestCase>();
        List<TestStep> steps = new ArrayList<TestStep>();
        // 每次循环必定产生一条测试步骤
        String name = null;
        String desc = null;
        String module = null;
        for (int i = 0; i < data.size(); i++) {
            TestStep step;
            String[] stepStringData = data.get(i);
            // 判断是否是新测试用例
            if (stepStringData[0] != null && !stepStringData[0].equals("")) {
                if (!steps.isEmpty()) {
                    // 每次循环都会检查步骤列表steps是否为空，如果非空而且用例编号存在，则表示出现一条新的用例
                    // 此时需要把上一条用例保存到列表中，并清空steps
                    //清空数据会导致引用该对象的列表也清空,因此此处使用浅拷贝复制一份对象
                    List<TestStep> testSteps = (List<TestStep>) ((ArrayList<TestStep>) steps).clone();
                    TestCaseFactory caseFactory = new TestCaseFactory(name, desc, module, testSteps);
                    BaseTestCase testCase = caseFactory.buildTestCase();
                    testCases.add(testCase);
                    steps.clear();  //清空测试步骤列表
                }

                // 获取名称，描述，模块，为下个循环保存值
                name = stepStringData[1];
                desc = stepStringData[2];
                module = stepStringData[3];
            }

            //产生一条测试步骤
            TestStepFactory stepFactory = new TestStepFactory();
            stepFactory.setActionType(stepStringData[4]);
            stepFactory.setElementSearchType(stepStringData[5]);
            stepFactory.setElementSearchValue(stepStringData[6]);
            stepFactory.setEnterValue(stepStringData[7]);
            stepFactory.setVerityKeyword(stepStringData[8]);
            stepFactory.setVertiyElementSearchType(stepStringData[9]);
            stepFactory.setVertiyElementSearchValue(stepStringData[10]);
            step = stepFactory.buildStep(driver);
            steps.add(step);

            // 防止data最后一个元素数据没有添加进入testcases
            if (i == data.size() - 1) {
                TestCaseFactory caseFactory = new TestCaseFactory(name, desc, module, steps);
                BaseTestCase testCase = caseFactory.buildTestCase();
                testCases.add(testCase);
            }

        }
        System.out.println(testCases);
        return testCases;
    }



    /**
     * 使用反射获取类对象
     *
     * @param className
     * @return
     */
    @Deprecated
    public Object classBuilder(String className) {
        try {
            Class c = Class.forName("com.dodol.common.UserAction." + className.toUpperCase());
            return c.newInstance();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return null;
    }


}
